package com.cloud.business.service.sys.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.business.dao.sys.SysMessageMapper;
import com.cloud.business.entity.sys.SysMessage;
import com.cloud.business.model.commonvm.DeletePost;
import com.cloud.business.model.commonvm.ResultMsg;
import com.cloud.business.model.sys.sysmessagevm.SysMessageQueryModel;
import com.cloud.business.service.sys.ISysMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * 系统消息表 服务实现类
 * </p>
 *
 * @author dh
 * @since 2020-03-25
 */
@Service
public class SysMessageServiceImpl extends ServiceImpl<SysMessageMapper, SysMessage> implements ISysMessageService {

    private final SysMessageMapper sysMessageMapper;

    @Autowired
    public SysMessageServiceImpl(SysMessageMapper sysMessageMapper) {
        this.sysMessageMapper = sysMessageMapper;
    }

    //region 获取分页列表

    /**
     * 获取分页列表
     *
     * @param sysMessageQueryModel
     * @param page
     * @param msg
     * @throws Exception
     */
    @Override
    public void getListByPage(SysMessageQueryModel sysMessageQueryModel, IPage<SysMessage> page, ResultMsg msg) throws Exception {
        QueryWrapper<SysMessage> sysMessageQueryWrapper = new QueryWrapper<>();
        sysMessageQueryWrapper.lambda()
                .like(!StringUtils.isEmpty(sysMessageQueryModel.getQueryStr()), SysMessage::getTitle, sysMessageQueryModel.getQueryStr())
                ;
        sysMessageMapper.selectPage(page, sysMessageQueryWrapper);
        msg.setMsg("获取成功");
    }
    //endregion

    //region 创建

    /**
     * 创建
     *
     * @param sysMessage
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public Integer create(SysMessage sysMessage, ResultMsg msg) throws Exception {
        Date now = new Date();
        sysMessage.setCreateTime(now);
        sysMessage.setModificationTime(now);
        int count = sysMessageMapper.insert(sysMessage);
        msg.setMsg("创建成功");
        return sysMessage.getObjectId();
    }
    //endregion

    //region 更新

    /**
     * 更新
     *
     * @param sysMessage
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public Integer update(SysMessage sysMessage, ResultMsg msg) throws Exception {
        Date now = new Date();
        sysMessage.setModificationTime(now);
        int count = sysMessageMapper.updateById(sysMessage);
        msg.setMsg("更新成功");
        return sysMessage.getObjectId();
    }
    //endregion

    //region 获取单条

    /**
     * 获取单条
     *
     * @param operationId
     * @param objectId
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public SysMessage getById(Integer operationId, Integer objectId, ResultMsg msg) throws Exception {
        SysMessage SysMessage = sysMessageMapper.selectById(objectId);
        msg.setMsg("获取成功");
        return SysMessage;
    }
    //endregion

    //region 批量删除

    /**
     * 批量删除
     *
     * @param deletePost
     * @param msg
     * @return
     * @throws Exception
     */
    @Override
    public int deleteByList(DeletePost deletePost, ResultMsg msg) throws Exception {
        msg.setMsg("删除成功");
        int count = sysMessageMapper.deleteBatchIds(deletePost.getKeyValues());
        if (count <= 0) {
            throw new Exception("删除失败");
        }
        return count;
    }
    //endregion

}
